const express = require("express")
const { ApiResponse, tokenMiddleware } = require('./api_response')
const fs = require("fs")
const router = express.Router()
const path = require('path')

// html 网页请求
router.get("/snakegame", (req, res) => {
    let filePath = path.join(process.cwd(),"/public/game/SnakeGame.html");
    let apiRes = new ApiResponse(res);
    fs.access(filePath, fs.constants.F_OK, (err) => {
        if (err) {
            //console.error(`文件不存在: ${filePath}`);
            apiRes.validateFailed(`文件不存在: ${filePath}`)
        } else {
            //1.直接重新指定访问地址
            //apiRes.redirect('/game/SnakeGame.html')

            //2.发送文件
            // let rootPath = process.cwd() + "/public/game/";
            // apiRes.sendFile('SnakeGame.html', rootPath)
            
            //3.直接发送
            res.sendFile(filePath);

            //4.发送二进制文件
            // res.statusCode = 200;
            // res.setHeader('Content-Type', 'text/html');
            // fs.readFile(filePath, (err, data) => {
            //   if (err) throw err;
            //   console.log(data.toString);
            //   res.end(data);
            // });
        }
    });
})

module.exports = router